package copy;

import ink.tiptop.common.GenericBuilder;
import ink.tiptop.constant.Rgx;
import ink.tiptop.common.worker.scan.LocalScanner;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author ink.tiptop.zjf
 * @Title: toMaven
 * @Package file
 * @Description: 拷贝tar.gz到统一位置用的
 * @date 2020-07-01 09:53
 */
public class FileCopy {

    /**
     * 全国版数票
     *
     * @throws Exception
     */
    @Test
    public void btsAli() throws Exception {
        final String absPath = "/Users/zhongjiafeng/Desktop/bwton/app/bts-cloud";
        final String destPath = "/Users/zhongjiafeng/Desktop/bwton/服务发布/数字票务/开发/";
        List<String> notUnderDirs = Arrays.asList("target");
        filter.set(file -> {
            for (String notUnderDir : notUnderDirs) {
                String parent = file.getParent();
                return parent.endsWith(notUnderDir);
            }
            return false;
        });
        filePattern.set(Rgx.GZ);
        run(Collections.singletonList(absPath), destPath);
        System.exit(0);
    }
    /**
     * 银联数票
     *
     * @throws Exception
     */
    @Test
    public void bts() throws Exception {
        final String absPath = "/Users/zhongjiafeng/Desktop/bwton/app/bts_unionpay";
        final String destPath = "/Users/zhongjiafeng/Desktop/bwton/服务发布/数字票务";
        List<String> notUnderDirs = Arrays.asList("target");
        filter.set(file -> {
            for (String notUnderDir : notUnderDirs) {
                String parent = file.getParent();
                return parent.endsWith(notUnderDir);
            }
            return false;
        });
        filePattern.set(Rgx.GZ);
        run(Collections.singletonList(absPath), destPath);
        System.exit(0);
    }



    /**
     * 常州行
     *
     * @throws Exception
     */
    @Test
    public void changzx() throws Exception {
        final String absPath = "/Users/zhongjiafeng/Desktop/bwton/app/xbsx-app-changzx";
        final String destPath = "/Users/zhongjiafeng/Desktop/bwton/服务发布/常州行/jar";
        List<String> notUnderDirs = Arrays.asList("target");
        filter.set(file -> {
            for (String notUnderDir : notUnderDirs) {
                String parent = file.getParent();
                return parent.endsWith(notUnderDir);
            }
            return false;
        });
        filePattern.set(Rgx.JAR_NAME);
        run(Collections.singletonList(absPath), destPath);
        System.exit(0);
    }

    /**
     * 实时公交
     *
     * @throws Exception
     */
    @Test
    public void realtimeBus() throws Exception {
        final String absPath = "/Users/zhongjiafeng/Desktop/bwton/app/realtime_bus";
        final String destPath = "/Users/zhongjiafeng/Desktop/bwton/服务发布/实时公交";
        filePattern.set(Rgx.GZ);
        run(Collections.singletonList(absPath), destPath);
        System.exit(0);
    }

    private static final ThreadLocal<String> filePattern = new ThreadLocal<>();

    private static ThreadLocal<Predicate<File>> filter = new ThreadLocal<>();


    @Test
    public void changzhouxing() throws Exception {
        final String absPath = "/Users/zhongjiafeng/Desktop/bwton/app/xbsx-app-changzx";
        final String destPath = "/Users/zhongjiafeng/Desktop/bwton/服务发布/changzx";
        List<String> notUnderDirs = Arrays.asList("target");
        filter.set(file -> {
            for (String notUnderDir : notUnderDirs) {
                String parent = file.getParent();
                return parent.endsWith(notUnderDir);
            }
            return false;
        });
        filePattern.set(Rgx.JAR_NAME);
        run(Collections.singletonList(absPath), destPath);
    }

    private static void run(List<String> sourceList, String dest) throws Exception {
        LocalScanner scanner = GenericBuilder
                .of(() -> new LocalScanner(sourceList, 5))
                .build();
        List<String> localJarList = scanner.scan(filePattern.get());
        List<File> files = localJarList.parallelStream().map(File::new).collect(Collectors.toList());
        copyFilesToDir(files, dest);
    }

    private static void copyFilesToDir(List<File> files, String dest) throws Exception {
        File destDir = new File(dest);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        for (File file : files) {
            Predicate<File> filePredicate = filter.get();
            boolean isUnderTarget = filePredicate.test(file);
            if (!isUnderTarget) {
                continue;
            }
            System.out.println("当前文件路径:"+file.getAbsolutePath());
            File outFile = new File(dest, file.getName());
            if (outFile.exists()) {
                outFile.delete();
            }
            outFile.createNewFile();
            copyFileUsingFileStreams(file, outFile);
        }
    }

    private static void copyFileUsingFileStreams(File source, File dest)
            throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            if (input != null) {
                input.close();
            }
            if (output != null) {
                output.close();
            }
        }
    }


}
